package com.rd.student.ruanjian.class3.gaochenxi.LianXi2.NumberEight;

import java.util.*;

public class NumberEight {

   public static void main(String[] args) {
      System.out.println(" 成绩单如下：");
      ArrayList<Score> list = returnArrayList();
      returnMax_and_Min(list);
      System.out.println("===================");
      returnScoreDun(list);
   }

   public static void returnScoreDun(ArrayList<Score> list) {
      String str1 = "";
      String str2 = "";
      for (Score s : list) {
         if (s.getScore() >= 60) {
            str1 = str1.concat(s.getId() + "\t\t" + s.getScore() + "\n");
         } else {
            str2 = str2.concat(s.getId() + "\t\t" + s.getScore() + "\n");
         }
      }
      System.out.println("以下为60分以上分数段的同学:");
      System.out.println("学号    成绩");
      System.out.println(str1);
      System.out.println("以下为60分以下分数段的同学:");
      System.out.println("学号    成绩");
      System.out.println(str2);
      System.out.println("班级所有同学的平均成绩：");
      returnAvg(list);
   }

   public static void returnAvg(ArrayList<Score> list) {
      double sum = 0.0;
      for (Score s : list) {
         sum += s.getScore();
      }
      System.out.println("全班的平均成绩为:" + sum / list.size());
   }

   public static void returnMax_and_Min(ArrayList<Score> list) {
      int max = 0, min = 100;
      int maxId = 0, minId = 0;
      for (Score s : list) {
         if (s.getScore() > max) {
            max = s.getScore();
            maxId = s.getId();
         }
         if (s.getScore() < min) {
            min = s.getScore();
            minId = s.getId();
         }
      }
      System.out.println("MinID: " + minId + "  分数为 :" + min);
      System.out.println("MaxID: " + maxId + " 分数为 :" + max);
   }

   public static ArrayList<Score> returnArrayList() {
      ArrayList<Score> list = new ArrayList<>();
      Random random = new Random();
      for (int i = 0; i < 20; i++) {
         int a = i + 1;
         int b = random.nextInt(101);
         list.add(new Score(a, b));
      }
      System.out.println("学号    成绩");
      for (Score s : list) {
         System.out.println(s.getId() + "\t\t" +
               s.getScore());
      }
      return list;
   }
}

class Score implements Comparable {
   private int id;
   private int score;

   public Score(int id, int grade, int score) {
   }

   public Score(int id, int score) {
      this.id = id;
      this.score = score;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public int getScore() {
      return score;
   }

   public void setScore(int score) {
      this.score = score;
   }


   @Override
   public String toString() {
      return "[" + id + "   " + score + "]";
   }

   @Override
   public int compareTo(Object o) {
      Score sco = (Score) o;
      if (this.getId() == sco.getId()) {
         return this.getId() - sco.getId();
      } else {
         return sco.getScore() - this.getScore();
      }
   }
}
